package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.PictureAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.PictureUpdateDTO;
import cn.tedu.store.commons.pojo.product.entity.Picture;
import cn.tedu.store.commons.pojo.product.vo.AlbumStandardVO;
import cn.tedu.store.commons.pojo.product.vo.PictureListItemVO;
import cn.tedu.store.commons.pojo.product.vo.PictureStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.AlbumMapper;
import cn.tedu.store.product.webapi.mapper.PictureMapper;
import cn.tedu.store.product.service.IPictureService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片业务接口实现类
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Service
@DubboService
public class PictureServiceImpl implements IPictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理[添加图片]业务 参数:{}", pictureAddNewDTO);
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO,picture);
        int rows = pictureMapper.insert(picture);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void addNewBatch(List<PictureAddNewDTO> list) {
        log.debug("开始处理[批量添加图片]业务 参数:{}", list);
        List<Picture> addList = new ArrayList<>();
        for (PictureAddNewDTO pictureAddNewDTO : list) {
            Picture picture = new Picture();
            BeanUtils.copyProperties(pictureAddNewDTO,picture);
            addList.add(picture);
        }
        int rows = pictureMapper.insertBatch(addList);
        if(rows != list.size()){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理[删除图片]业务 参数:{}", id);
        isExists(id);
        int rows = pictureMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        log.debug("开始处理[批量删除图片]业务 参数:{}", ids);
        int rows = pictureMapper.deleteByIds(ids);
        if(rows != ids.length){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void setIsCover(Long id) {
        log.debug("开始处理[设为封面]业务 参数:{}", id);
        updateStatusById(id,1);
    }

    @Override
    public void setNotCover(Long id) {
        log.debug("开始处理[撤销封面]业务 参数:{}", id);
        updateStatusById(id,0);
    }

    @Override
    public void update(Long id, PictureUpdateDTO pictureUpdateDTO) {
        log.debug("开始处理[修改详情]业务 参数:{},{}", id, pictureUpdateDTO);
        isExists(id);
        //执行修改
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureUpdateDTO,picture);
        picture.setId(id);
        int rows = pictureMapper.updateById(picture);
        if (rows != 1) {
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public int countByAlbum(Long albumId) {
        log.debug("开始处理[统计图片数量]业务 参数:{}", albumId);
        AlbumStandardVO result = albumMapper.getStandardById(albumId);
        if (result == null) {
            String message = "获取失败,相册不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return pictureMapper.countByAlbum(albumId);
    }

    @Override
    public PictureStandardVO getStandardById(Long id) {
        log.debug("开始处理[获取图片详情]业务 参数:{}", id);
        isExists(id);
        return pictureMapper.getStandardById(id);
    }

    @Override
    public List<PictureListItemVO> listByAlbum(Long albumId) {
        log.debug("开始处理[获取图片列表]业务 参数:{}", albumId);
        AlbumStandardVO result = albumMapper.getStandardById(albumId);
        if (result == null) {
            String message = "获取图片列表失败,相册不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return pictureMapper.listByAlbum(albumId);
    }

    private void updateStatusById(Long id, Integer enable) {
        String[] tips = {"撤销封面", "设为封面"};
        PictureStandardVO result = pictureMapper.getStandardById(id);
        if (result == null) {
            String message = tips[enable] + "失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (result.getIsCover() == enable) {
            String message = tips[enable] + "失败,该图片已经被[" + tips[enable]+"]";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //执行修改
        Picture picture = new Picture();
        picture.setId(id);
        picture.setIsCover(enable);
        int rows = pictureMapper.updateById(picture);
        if (rows != 1) {
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    /**
     * 判断图片是否存在
     * @param id 图片id
     */
    private void isExists(Long id){
        PictureStandardVO result = pictureMapper.getStandardById(id);
        if (result == null) {
            String message = "修改图片信息失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
    }
}
